<template>
  <div class="box">
    <!-- 搜索 -->
    <van-search v-model="keyword"
                shape="round"
                background="#FFFFFF"
                placeholder="搜索" />

    <van-pull-refresh style="min-height: 80vh;"
                      v-model="refreshing"
                      @refresh="onRefresh">
      <van-list v-model="loading"
                :finished="finished"
                finished-text="END"
                @load="getData">
        <div class="list">
          <div class="items"
               :key="ins"
               @click="detail(i.id)"
               v-for="(i, ins) in lists">
            <div class="info">
              <span>{{i.nickname}}</span>
              <span class="right">{{i.mobile}}</span>
            </div>
            <div class="flex-box">
              <div class="num">住院号：{{i.admission_number}}</div>
              <div class="carcinoma">癌种：{{i.carcinoma}}</div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>

  </div>
</template>
<script>
import { beenAssociation } from '@/api/medical'
export default {
  data () {
    return {
      keyword: '',
      refreshing: false,
      loading: false,
      finished: false,
      list: [],
      parameter: {
        page_index: 0,
        page_size: 20
      }
    }
  },
  computed: {
    lists () {
      return this.list.filter(it => it.nickname.includes(this.keyword))
    }
  },
  methods: {
    // 患者详情
    detail (id) {
      this.$router.push('/user/userDetail?id=' + id)
    },
    // 下拉刷新
    onRefresh () {
      // TODO 刷新一定要重置页码
      this.parameter.page_index = 0
      this.finished = false
      this.loading = true
      this.getData()
    },
    async getData () {
      // TODO TEST vant-list 分页逻辑
      this.parameter.page_index += 1
      if (this.refreshing) {
        this.list = []
        this.refreshing = false
      }
      const { data, code, msg } = await beenAssociation(this.parameter)
      this.loading = false
      if (code !== 1) {
        this.finished = true
        return this.$toast.fail(msg)
      }
      if (this.parameter.page_index === 1) this.list = []
      this.list.push(...data.data || [])
      if (this.list.length >= data.total) {
        this.finished = true
      }
    }
  }
}
</script>
<style scoped lang='less'>
.icon_right {
  width: 12px;
  height: 21px;
  vertical-align: middle;
}
.box {
  width: 100%;
  /deep/.van-search__content {
    border: 1px solid #3f78f9;
  }
  /deep/.van-icon-search {
    color: #3f78f9;
  }
  /deep/.van-field__control {
    color: #3f78f9;
  }
  ::placeholder {
    color: #3f78f9 !important;
  }
  .list {
    width: 100%;
    .items {
      width: 100%;
      // height: 105px;
      padding: 25px 30px;
      box-sizing: border-box;
      background: url("../../assets/img/user_bj1.png") no-repeat;
      background-size: 100% 100%;
      // background-color: red;
    }
    .active {
      background: url("../../assets/img/user_bj2.png") no-repeat;
      background-size: 100% 100%;
    }
    .flex-box {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: baseline;
      .carcinoma {
        font-size: 13px;
        width: 100px;
        margin-right: -8px;
        padding-bottom: 15px;
        color: #ffffff;
      }
    }
    .info {
      width: 100%;
      height: 25px;
      font-size: 15px;
      font-family: PingFang SC;
      font-weight: 600;
      line-height: 30px;
      color: #ffffff;
      .right {
        float: right;
        color: #ffffff;
        font-weight: 400;
      }
    }
    .num {
      height: 18px;
      font-size: 13px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 30px;
      color: #ffffff;
    }
  }
}
</style>
